package com.jaya.budan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jaya.budan.R;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.views.Circle_ImageView;

import java.util.List;

public class GeRen_Main_Fans_ListView_Adapter extends BaseAdapter {

    private Context mcontext;
    private List<User_Limit> mlist;
    private int mtype, m_visit;
    public GeRen_Main_Fans_ListView_Adapter(Context context, List<User_Limit> list, int type, int visit){
        mcontext = context;
        mlist = list;
        mtype = type;
        m_visit = visit;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"SetTextI18n", "ViewHolder"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mtype == 2){
            convertView = View.inflate(mcontext, R.layout.nearby_listview_item, null);
            TextView tv_nearby_listview_item_name = convertView.findViewById(R.id.tv_nearby_listview_item_name);
            TextView tv_nearby_listview_item_gxqm = convertView.findViewById(R.id.tv_nearby_listview_item_gxqm);
            TextView tv_nearby_listview_item_height = convertView.findViewById(R.id.tv_nearby_listview_item_height);
            TextView tv_nearby_listview_item_juli = convertView.findViewById(R.id.tv_nearby_listview_item_juli);
            TextView tv_nearby_listview_item_online = convertView.findViewById(R.id.tv_nearby_listview_item_online);
            RelativeLayout rl_nearby_item = convertView.findViewById(R.id.rl_nearby_item);
            RelativeLayout rl_nearby_item_jile = convertView.findViewById(R.id.rl_nearby_item_jile);
            Button btn_main_juli = convertView.findViewById(R.id.btn_main_juli);
            Button btn_main_online = convertView.findViewById(R.id.btn_main_online);
            Circle_ImageView iv_nearby_item_icon = convertView.findViewById(R.id.iv_nearby_item_icon);
            ImageView iv_nearby_item_isvip = convertView.findViewById(R.id.iv_nearby_item_isvip);
            ImageView iv_nearby_item_sex = convertView.findViewById(R.id.iv_nearby_item_sex);
            ImageView iv_chat_item_online = convertView.findViewById(R.id.iv_chat_item_online);
            Glide.with(mcontext).load(mlist.get(position).getHead_path()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(iv_nearby_item_icon);
            tv_nearby_listview_item_name.setText("" + mlist.get(position).getNicename());
            if (m_visit == 1){
                tv_nearby_listview_item_gxqm.setText("" + mlist.get(position).getTime() + "看过我");
            } else if (m_visit == 2){
                tv_nearby_listview_item_gxqm.setText("" + mlist.get(position).getTime() + "看过他");
            } else {
                rl_nearby_item.setVisibility(View.GONE);
                rl_nearby_item_jile.setVisibility(View.VISIBLE);
                btn_main_juli.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        btn_main_juli.setText("已同意");
                        btn_main_juli.setTextColor(Color.parseColor("#999999"));
                        btn_main_juli.setBackgroundColor(Color.parseColor("#ffffff"));
                    }
                });
                btn_main_online.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        btn_main_online.setText("已忽略");
                        btn_main_online.setTextColor(Color.parseColor("#999999"));
                        btn_main_online.setBackgroundColor(Color.parseColor("#ffffff"));
                    }
                });
                tv_nearby_listview_item_gxqm.setText("" + mlist.get(position).getTime() + "申请访问你的相册");
            }
            tv_nearby_listview_item_height.setText("" + mlist.get(position).getAge() + "/" + mlist.get(position).getHeight() + "/" + mlist.get(position).getWeigh());
            if (mlist.get(position).getSex().equals("1")){ //男
                iv_nearby_item_sex.setImageResource(R.mipmap.boy);
            } else {
                iv_nearby_item_sex.setImageResource(R.mipmap.girl);
            }
            if (mlist.get(position).getIs_vip() == 1){
                iv_nearby_item_isvip.setVisibility(View.VISIBLE);//是 会员
            } else {
                iv_nearby_item_isvip.setVisibility(View.INVISIBLE);
            }
            if (mlist.get(position).getIs_online() == 1){ //离线
                iv_chat_item_online.setVisibility(View.GONE);
            } else {
                iv_chat_item_online.setVisibility(View.VISIBLE);
            }
            tv_nearby_listview_item_juli.setText("" + mlist.get(position).getDistance());
            tv_nearby_listview_item_online.setVisibility(View.GONE);
            return convertView;
        } else {
            convertView = View.inflate(mcontext, R.layout.geren_main_fans_listview_item, null);
            TextView tv_nearby_listview_item_name = convertView.findViewById(R.id.tv_nearby_listview_item_name);
            TextView tv_nearby_listview_item_gxqm = convertView.findViewById(R.id.tv_nearby_listview_item_gxqm);
            TextView tv_nearby_listview_item_height = convertView.findViewById(R.id.tv_nearby_listview_item_height);
            Button tv_nearby_listview_item_online = convertView.findViewById(R.id.tv_nearby_listview_item_online);
            Circle_ImageView iv_nearby_item_icon = convertView.findViewById(R.id.iv_nearby_item_icon);
            ImageView iv_nearby_item_isvip = convertView.findViewById(R.id.iv_nearby_item_isvip);
            ImageView iv_nearby_item_sex = convertView.findViewById(R.id.iv_nearby_item_sex);
            Glide.with(mcontext).load(mlist.get(position).getHead()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(iv_nearby_item_icon);
            tv_nearby_listview_item_name.setText("" + mlist.get(position).getNicename());
            if (mtype == 1) {
                tv_nearby_listview_item_online.setBackgroundResource(R.drawable.login_login);
                tv_nearby_listview_item_online.setText("取消关注");
            } else {
                if (mlist.get(position).getIs_online() == 1){
                    tv_nearby_listview_item_online.setBackgroundResource(R.mipmap.iv_geren_fans_1);
                    tv_nearby_listview_item_online.setText("被关注");
                } else {
                    tv_nearby_listview_item_online.setBackgroundResource(R.mipmap.iv_geren_fans_2);
                    tv_nearby_listview_item_online.setText("互相关注");
                }
            }
            tv_nearby_listview_item_gxqm.setText("" + mlist.get(position).getRemark());
            tv_nearby_listview_item_height.setText("" + mlist.get(position).getAge() + "/" + mlist.get(position).getHeight() + "/" + mlist.get(position).getWeigh()+ "/" + mlist.get(position).getRole());
            if (mlist.get(position).getSex().equals("1")){ //男
                iv_nearby_item_sex.setImageResource(R.mipmap.boy);
            } else {
                iv_nearby_item_sex.setImageResource(R.mipmap.girl);
            }
            if (mlist.get(position).getIs_vip() == 1){
                iv_nearby_item_isvip.setVisibility(View.VISIBLE);//是 会员
            } else {
                iv_nearby_item_isvip.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }
    }
}
